<template>
  <div class="lb-curtomer-talking-classify">
    <top-nav></top-nav>
    <div class="page-main">
      <el-row class="page-top-operate">
        <lb-button
          type="primary"
          icon="el-icon-plus"
          @click="editClassifyItem('add')"
          >添加话术分类</lb-button
        >
      </el-row>
      <div class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="分类名" prop="title">
            <el-input
              v-model="searchForm.title"
              placeholder="请输入分类名"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column
          prop="title"
          label="分类名"
          width="180"
        ></el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="是否开启">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="updateTalkClassifyInfo(scope.row.id, scope.row.status)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="
                  editClassifyItem('edit', {
                    id: scope.row.id,
                    top: scope.row.top,
                    title: scope.row.title
                  })
                "
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="delClassifyItem(scope.row.$index, scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      ></lb-page>
    </div>
    <el-dialog
      :title="dialogForm.id ? '编辑分类' : '添加分类'"
      :visible.sync="centerDialogVisible"
      width="500px"
      center
    >
      <el-form class="dialog-form" :model="dialogForm" label-width="80px">
        <el-form-item label="分类名">
          <el-input
            v-model="dialogForm.title"
            placeholder="请输入分类名"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序值">
          <el-input-number
            class="lb-input-number"
            v-model="dialogForm.top"
            :controls="false"
            :precision="0"
            :min="0"
            placeholder="请输入排序值"
          ></el-input-number>
          <lb-tool-tips>值越大, 排序越靠前</lb-tool-tips>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitDialogForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { reg } from '@/utils/reg'
export default {
  data () {
    return {
      loading: false,
      searchForm: {
        title: '',
        page: 1,
        limit: 10,
        page_count: 10
      },
      tableData: [],
      limit: 10,
      page: 1,
      total: 0,
      centerDialogVisible: false,
      dialogForm: {
        title: '',
        top: '',
        id: ''
      }
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let { searchForm } = this
      searchForm.page_count = searchForm.limit
      this.$api.getCustomerTalkClassifyList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    editClassifyItem (type, formInfo) {
      this.dialogForm = type === 'add' ? { title: '', top: 0 } : formInfo
      this.centerDialogVisible = true
    },
    delClassifyItem (index, id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.$api.delCustomerTalkClassifyInto({ type_id: id }).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successDel'))
            this.searchForm.page =
              this.searchForm.page <
                Math.ceil((this.total - 1) / this.searchForm.limit)
                ? this.searchForm.page
                : Math.ceil((this.total - 1) / this.searchForm.limit)
            this.getTableDataList()
          }
        })
      })
    },
    submitDialogForm () {
      let { dialogForm } = this
      if (!dialogForm.title) {
        this.$message.error('请输入分类名')
      } else if (dialogForm.top === '' || !reg.number.test(dialogForm.top)) {
        this.$message.error('请输入不小于0的数字')
      } else {
        let urlType = dialogForm.id
          ? 'editCustomerTalkClassifyInfo'
          : 'addCustomerTalkClassifyInfo'
        this.$api[urlType]({ data: dialogForm }).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successSub'))
            this.centerDialogVisible = false
            this.getTableDataList()
          }
        })
      }
    },
    updateTalkClassifyInfo (id, status) {
      this.$api
        .editCustomerTalkClassifyInfo({ data: { id, status } })
        .then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successOper'))
          } else {
            this.getTableDataList()
          }
        })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-curtomer-talking-classify {
  width: 100%;
  .page-main {
    width: 100%;
  }
  .el-input {
    width: 200px;
  }
  .dialog-form {
    .el-input,
    .el-input-number {
      width: 300px;
    }
  }
}
</style>
